import { 
	ApiLiaisonList,
	// ApiLiaisonTag,
	ApiLiaisonPeople,
	ApiMyCouponList,
	ApiUpdateMemberUser,
	ApidDeleteMemberUser,
	ApiaddComplaint,
	ApiFindMsgPage,
	ApiChangeMemberBaseInfo,
	ApiGetMemberInfo,
	ApiCouponCount } from '@/common/request/api/person.js'
import {
	MEMBER_COUPONLIST,
	MEMBER_COUPONLOADING_STATUS,
	LIAISONLIST,
	FINDMSGPAGE,
	USERINFORMATION,
	MEMBERINFO,
	COUPONCOUNT
} from "../types.js"
const state = {
	liaisonList:[],
	findmsgpageList:[],
	userinformation:[],
	memberInfo:{},
	couponCount:[],
	memberCouponList:{
		records:[],
		data:[],
		current:1,
		pages:0,
		size:20,
		total:0
	},
	memberCouponLoadingStatus:'loading',
}
const actions = {
	// 完善用户个人基础信息
	async ActChangeMemberBaseInfo({dispatch,commit},payload){
		const res = await ApiChangeMemberBaseInfo(payload)
		if(res.success){
			commit('USERINFORMATION',res.data)
		}else{
			uni.showModal({
				title: '提示',
				content: res.msg,
				showCancel:false
			});
		}
	},
	// 获取用户信息
	async ActGetMemberInfo({dispatch,commit},payload){
		const res = await ApiGetMemberInfo(payload)
		if(res.success){
			const phone  = res.data.memberMobile 
			const modifyPhone = phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
			res.data.memberMobile = modifyPhone
			commit('MEMBERINFO',res.data)
		}
	},
	// 获取优惠卷数量
	async AcTGetCouponCount({dispatch,commit},payload){
		const res = await ApiCouponCount(payload)
		console.log("优惠券",res)
		if(res.success){
			commit('COUPONCOUNT',res.data)
		}
	},
	//获取联系人
	async ActGetLiaisonList({dispatch,commit},payload){
		const res = await ApiLiaisonList(payload)
		if(res.success){
			commit('LIAISONLIST',res.data)
		}else{
			uni.showModal({
				title: '提示',
				content: res.msg,
				showCancel:false
			});
		}
	},
	// 获取标签
	// async ActLiaisonTag({dispatch,commit},payload){
	// 	const reqbody = {dictType:'drug_relation',isCtr:'0'}
	// 	return await ApiLiaisonTag(reqbody)
	// },
	// 获取帮助说明
	async ActGetFindMsgPage ({dispatch,commit},payload){
		const res = await ApiFindMsgPage(payload)
		if(res.success){
			commit('FINDMSGPAGE',res.data.records)
		}else{
			uni.showModal({
				title: '提示',
				content: res.msg,
				showCancel:false
			});
		}
	},
	
	// 新增联系人
	async ActLiaisonPeople({dispatch,commit},payload){
		const res = await ApiLiaisonPeople(payload)
		return res
	},
	// 修改联系人
	async ActUpdateMemberUser({commit},payload){
		const res = await ApiUpdateMemberUser(payload)
		return res
	},
	// 删除联系人
	async ActDeleteMemberUser({commit},payload){
		const res = await ApidDeleteMemberUser(payload)
		return res
	},
	// 意见反馈
	async ActPostaddComplaint({commit},payload){
		const res = await ApiaddComplaint(payload)
		return res
	},
	// 已领取优惠券列表
	async ActGetMyCouponList({commit},payload){
		commit('MEMBER_COUPONLOADING_STATUS','loading')
		if (payload.current===1) {
			commit('MEMBER_COUPONLIST','reset')	
		}
		const res = await ApiMyCouponList(payload)
		if(res.success){
			commit('MEMBER_COUPONLIST',res.data)
		}
	},
	

}
const mutations ={
	[MEMBER_COUPONLIST](state,data){
		if (data==='reset') {
			state.memberCouponList = {
				records:[]
			}
		}else {
			if(data.current === 1){
				data.data = data.records
			}else{
				data.data = state.memberCouponList.data.concat(data.records)
			}
			if(data.current*data.size<data.total){
				state.memberCouponLoadingStatus = 'loadmore'
			}else{
				state.memberCouponLoadingStatus = "nomore"
			}
			state.memberCouponList = data
		}
	},
	[MEMBER_COUPONLOADING_STATUS](state, data){
		state.memberCouponLoadingStatus = data
	},
	[LIAISONLIST](state, data){
		state.liaisonList = data
	},
	[FINDMSGPAGE](state,data){
		state.findmsgpageList = data
	},
	[USERINFORMATION](state,data){
		state.userinformation = data
	},
	[MEMBERINFO](state,data){
		state.memberInfo = data
	},
	[COUPONCOUNT](state,data){
		state.couponCount = data
	}
}
const getters = {
	
}

export default {
	state,
	mutations,
	actions,
	getters
}